//
//  EeNavBar.swift
//  Alamofire
//
//  Created by zhu xietong on 2018/5/18.
//

import Foundation
import Eelay


extension UIViewController{
    public var eenavbar:MoBackView{
        get {
            if let obj = __extention["eenavbar"] as? MoBackView
            {
                return obj
            }
            let obj = MoBackView()
            self.eenavbar = obj
            view.addSubview(obj)
            return obj
            
        }
        set {
//            view.addSubview(newValue)
//            view.eelay = [
//                [newValue,[ee.T.L.R]]
//            ]
//            let navHeight = NSLayoutConstraint(item: newValue, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: TP.topHeight)
            
//            view.addConstraint(navHeight)
            
            __extention["eenavbar"] = newValue
//            __extention["eenavbar_height"] = navHeight
        }
    }
    
    private struct EeTopStyleAssociatedKeys {
        static var navbar = "EeTopStyleAssociatedKeys"
    }
    
    
    public func update(topStyle:EeTopStyle) {
        eenavbar.style = topStyle
    }
    
    @objc open func justEenavbarFrame() {
        
    }
  
    public func bringEeNavbar() {
        if let _ = self as? EeTopStyleSupport
        {
            if EeTopStyle.mode == .eenav{
                if top_style.isValid
                {
                    //                if let _ = self.navigationController as? EeNavigationController
                    //                {
                    //                    if let _ = self as? TypeInitController{
                    
                    if let backgroundV = self.navigationController?.navigationBar.value(forKeyPath: "_backgroundView") as? UIView
                    {
                        //                            var rect = backgroundV.superview!.convert(backgroundV.frame, to: self.view)
                        //                            eenavbar.frame = rect
                        if var rect = backgroundV.superview?.convert(backgroundV.frame, to: self.view)
                        {
                            rect = CGRect(x: 0, y: rect.origin.y, width: view.frame.size.width, height: rect.height)
                            eenavbar.frame = rect
                            view.bringSubviewToFront(eenavbar)
                            justEenavbarFrame()
                        }
//                        var rect = backgroundV.superview!.convert(backgroundV.frame, to: self.view)
                        
                    }
                    //                UIView *backgroundView = [self.navigationController.navigationBar valueForKey:@"_backgroundView"];
                    //                CGRect rect = [backgroundView.superview convertRect:backgroundView.frame toView:self.view];
                    //                self.km_transitionNavigationBar.frame = rect;
                    //
                    update(topStyle: top_style)
                    //                    }
                    //                }
                }
                else{
                    eenavbar.removeFromSuperview()
                }

            }
        }
        
    }
    
}
